(*
This file is part of the Lustre reader

* Copyright (c) 2010, Nicola Camuffo.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of Nicola Camuffo, nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)


open Lustypes


exception Expression_evaluation_exception of string



let _plus e1 e2 = match e1,e2 with
  E_INT(x),E_INT(y) -> E_INT(x+y)
  | E_REAL(x),E_REAL(y) -> E_REAL(x+.y)
  | _,_ -> raise (Expression_evaluation_exception "not numeric values")

let _minus e1 e2 = match e1,e2 with
  E_INT(x),E_INT(y) -> E_INT(x-y)
  | E_REAL(x),E_REAL(y) -> E_REAL(x-.y)
  | _,_ -> raise (Expression_evaluation_exception "not numeric values")

let _mult e1 e2 = match e1,e2 with
  E_INT(x),E_INT(y) -> E_INT(x*y)
  | E_REAL(x),E_REAL(y) -> E_REAL(x*.y)
  | _,_ -> raise (Expression_evaluation_exception "not numeric values")

let _div e1 e2 = match e1,e2 with
  E_INT(x),E_INT(y) -> E_INT(x/y)
  | E_REAL(x),E_REAL(y) -> E_REAL(x/.y)
  | _,_ -> raise (Expression_evaluation_exception "not numeric values")

let _intdiv e1 e2 = match e1,e2 with
  E_INT(x),E_INT(y) -> E_INT(x/y)
  | _,_ -> raise (Expression_evaluation_exception "not integer values")

let _mod e1 e2 = match e1,e2 with
  E_INT(x),E_INT(y) -> E_INT(x mod y)
  | _,_ -> raise (Expression_evaluation_exception "not integer values")

let _uminus e = match e with
  E_INT(x) -> E_INT(-x)
  | E_REAL(x) -> E_REAL(-.x)
  | _ -> raise (Expression_evaluation_exception "not numeric values")

let _and e1 e2 = match e1,e2 with
  E_BOOL(x),E_BOOL(y) -> E_BOOL(x&&y)
  | _,_ -> raise (Expression_evaluation_exception "not boolean values")

let _or e1 e2 = match e1,e2 with
  E_BOOL(x),E_BOOL(y) -> E_BOOL(x||y)
  | _,_ -> raise (Expression_evaluation_exception "not boolean values")

let _not e = match e with
  E_BOOL(x) -> E_BOOL(not x)
  | _ -> raise (Expression_evaluation_exception "not boolean values")

let _impl e1 e2 = match e1,e2 with
  E_BOOL(x),E_BOOL(y) -> E_BOOL((not x)||y)
  | _,_ -> raise (Expression_evaluation_exception "not boolean values")

let _xor e1 e2 = match e1,e2 with
  E_BOOL(x),E_BOOL(y) -> E_BOOL((x&&(not y))||((not x)&&y))
  | _,_ -> raise (Expression_evaluation_exception "not boolean values")

let _eq e1 e2 = match e1,e2 with
  E_INT(x),E_INT(y) -> E_BOOL(x==y)
  | E_REAL(x),E_REAL(y) -> E_BOOL(x==y)
  | E_BOOL(x),E_BOOL(y) -> E_BOOL(x==y)
  | _,_ -> raise (Expression_evaluation_exception "not simple (int,real,bool) values")

let _uneq e1 e2 = match e1,e2 with
  E_INT(x),E_INT(y) -> E_BOOL(x<>y)
  | E_REAL(x),E_REAL(y) -> E_BOOL(x<>y)
  | E_BOOL(x),E_BOOL(y) -> E_BOOL(x<>y)
  | _,_ -> raise (Expression_evaluation_exception "not simple (int,real,bool) values")

let _lt e1 e2 = match e1,e2 with
  E_INT(x),E_INT(y) -> E_BOOL(x<y)
  | E_REAL(x),E_REAL(y) -> E_BOOL(x<y)
  | E_BOOL(x),E_BOOL(y) -> E_BOOL(x<y)
  | _,_ -> raise (Expression_evaluation_exception "not simple (int,real,bool) values")

let _gt e1 e2 = match e1,e2 with
  E_INT(x),E_INT(y) -> E_BOOL(x>y)
  | E_REAL(x),E_REAL(y) -> E_BOOL(x>y)
  | E_BOOL(x),E_BOOL(y) -> E_BOOL(x>y)
  | _,_ -> raise (Expression_evaluation_exception "not simple (int,real,bool) values")

let _lte e1 e2 = match e1,e2 with
  E_INT(x),E_INT(y) -> E_BOOL(x<=y)
  | E_REAL(x),E_REAL(y) -> E_BOOL(x<=y)
  | E_BOOL(x),E_BOOL(y) -> E_BOOL(x<=y)
  | _,_ -> raise (Expression_evaluation_exception "not simple (int,real,bool) values")

let _gte e1 e2 = match e1,e2 with
  E_INT(x),E_INT(y) -> E_BOOL(x>=y)
  | E_REAL(x),E_REAL(y) -> E_BOOL(x>=y)
  | E_BOOL(x),E_BOOL(y) -> E_BOOL(x>=y)
  | _,_ -> raise (Expression_evaluation_exception "not simple (int,real,bool) values")

(*
let _toint e = match e with
  E_REAL(x) -> E_INT(int_of_float x)
  | _ -> raise (Expression_evaluation_exception "not real values")

let _toreal e = match e with
  E_INT(x) -> E_REAL(float_of_int x)
  | _ -> raise (Expression_evaluation_exception "not integer values")

let _tobool e = match e with
  E_INT(x) -> E_BOOL(x<>0)
  | E_REAL(x) -> E_BOOL(x<>0.)
  | _ -> raise (Expression_evaluation_exception "not numeric values")
*)

let _bool_value e = match e with
  E_BOOL(x) -> x
  | _ -> raise (Expression_evaluation_exception "not boolean values")
